<?php
/**
 * Created by PhpStorm.
 * User: Windows
 * Date: 2015/11/18
 * Time: 14:02
 */
class SearchModel extends BaseModel
{
    private $_table;
    protected $_name = 'iq_quotation';
    protected $_isbug = false;
    public function __construct() {
        parent::__construct();
    }

    public function getPriceCountByWhere($where,$group,$having){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        if(!$where){
            $where=" 1=1 ";
        }
//        $key   = parent::_getModuleCacheKey("getPriceCount_principal");
//        //get  Cache
//        $data = $this->_cache_handler->load($key);
//        if (!empty($data))
//        {
//            return $data;
//        }
        $sql = $db->select ()->from ( "iq_quotation as iq", array("product_id","count(*) as num") )
            ->joinLeft("users_invoice as ui","iq.business_id = ui.id","")
            ->joinLeft("product as p","iq.product_id = p.id","")
            ->joinLeft("product_cates as pc","pc.id = p.cate_id","")
            ->joinLeft("product_brands as pb","pb.id = p.brand_id","")
            ->joinLeft("region as r","r.region_id = iq.area_id","")
            ->joinLeft("res_currency as rc","rc.id = iq.currency_id","")
            ->where($where)->__toString ();

        if($group){
            $sql.= " group  by ".$group;
        }
        if($having){
            $sql.=" HAVING {$having}";
        }

        $sql = "select count(*) from(".$sql.")  as total";
        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        $data = $db->fetchOne ( $sql );
//        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }

    public function getPriceListByWhere($where,$item_page,$offset,$order,$group,$having){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        if(!$where){
            $where=" 1=1 ";
        }
//        $key   = parent::_getModuleCacheKey("getPriceList_principal".$offset);
//        //get  Cache
//        $data = $this->_cache_handler->load($key);
//        if (!empty($data))
//        {
//            return $data;
//        }
        $sql = $db->select ()->from ( "iq_quotation as iq", array("id","product_id","min(price) as price","num","ifnull(currency_id,1) as currency_id") )
            ->joinLeft("users_invoice as ui","iq.business_id = ui.id",array("business_name","contacts","mobile"))
            ->joinLeft("product as p","iq.product_id = p.id","product_name")
            ->joinLeft("product_cates as pc","pc.id = p.cate_id","cate_name")
            ->joinLeft("product_brands as pb","pb.id = p.brand_id","brand_name")
            ->joinLeft("region as r","r.region_id = iq.area_id","region_name as area_name")
            ->joinLeft("res_currency as rc","rc.id = iq.currency_id","name as currency_name")
            ->where($where)
            ->__toString ();
        if($group){
            $sql.= " group  by ".$group;
        }

        if($having){
            $sql.=" HAVING {$having}";
        }

        if($order){
            $sql.= " order  by ".$order;
        }
        if($item_page){
            $sql.= " limit $offset,$item_page";
        }

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        $data = $db->fetchAll ( $sql );
//        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }

    public function getQuotationPriceListByWhere($where ="1=1",$order ="id DESC"){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        if(!$where){
            $where=" 1=1 ";
        }
        $sql = $db->select ()->from ( "iq_quotation as iq", array("id","product_id","price","num","update_time","type","delivery_time","delivery_area","iq_contacts","iq_mobile","iq_qq") )
            ->joinLeft("users_invoice as ui","iq.business_id = ui.id",array("business_name","ifnull(contacts,iq.iq_contacts) as contacts","ifnull(mobile,iq.iq_mobile) as mobile","ifnull(qq,iq.iq_qq) as qq"))
            ->joinLeft("product as p","iq.product_id = p.id","product_name")
            ->joinLeft("product_cates as pc","pc.id = p.cate_id","cate_name")
            ->joinLeft("product_brands as pb","pb.id = p.brand_id","brand_name")
            ->joinLeft("region as r","r.region_id = iq.area_id","ifnull(region_name,0) as area_name")
            ->joinLeft("res_currency as rc","rc.id = iq.currency_id","name as currency_name")
            ->where($where)->__toString ();

        if($order){
            $sql.= " order  by ".$order;
        }

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        $data = $db->fetchAll ( $sql );
        if($data){
            foreach($data as $k=>$v){
                $data[$k]['business_name'] = empty($data[$k]['business_name'])?'未知公司' :$data[$k]['business_name'];
                $data[$k]['contacts'] = empty($data[$k]['contacts'])?'未知联系人' :$data[$k]['contacts'];
                $data[$k]['mobile'] = empty($data[$k]['mobile'])?'未知手机号码':$data[$k]['mobile'];
                $data[$k]['qq'] = empty($data[$k]['qq'])?'' :$data[$k]['qq'];
                $data[$k]['update_time'] = Tools::time2Units(time() - strtotime($v['update_time'])) . "前";
            }
        }
        return $data;
    }

    /**
     * @param string $where
     * @return array
     */
    public function getPriceBusinessByWhere($where ="1=1"){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        if(!$where){
            $where=" 1=1 ";
        }

        $sql = $db->select ()->from ( "iq_quotation as iq", "count(*)" )
            ->joinLeft("product as p","iq.product_id = p.id","product_name")
            ->joinLeft("product_cates as pc","pc.id = p.cate_id","cate_name")
            ->joinLeft("product_brands as pb","pb.id = p.brand_id","brand_name")
            ->joinLeft("region as r","r.region_id = iq.area_id","region_name as area_name")
            ->joinLeft("res_currency as rc","rc.id = iq.currency_id","name as currency_name")
            ->group("iq.business_id")
            ->where($where)->__toString ();
        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        $data = $db->fetchAll( $sql );
        return $data;
    }

    /**
     * @param string $where
     * @return string
     */
    public function getCateShow($where ="1=1"){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        if(!$where){
            $where=" 1=1 ";
        }
        $key   = parent::_getModuleCacheKey("getCateShow");
        //get  Cache
        $data = $this->_cache_handler->load($key);
        if (!empty($data))
        {
            return $data;
        }

        $sql = $db->select ()->from("product_cates",array("id","cate_name","vice_name","index_show"))->where($where)->__toString ();

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }

        $data = $db->fetchAll( $sql );

        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }

    /**
     * @param string $where
     * @return array
     */
    public function getCurrencyShow($where ="1=1"){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        if(!$where){
            $where=" 1=1 ";
        }
        $key   = parent::_getModuleCacheKey("getCurrencyShow");
        //get  Cache
        $data = $this->_cache_handler->load($key);
        if (!empty($data))
        {
            return $data;
        }

        $sql = $db->select ()->from("res_currency",array("id","name","code","mark"))->where($where)->__toString ();

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }

        $data = $db->fetchAll( $sql );

        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }

    public function getAreaShow($where ="1=1"){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        if(!$where){
            $where=" 1=1 ";
        }
        $key   = parent::_getModuleCacheKey("getAreaShow");
        $data = $this->_cache_handler->load($key);
        if (!empty($data))
        {
            return $data;
        }

        $sql = $db->select ()->from("iq_quotation as iq",array("area_id",))
            ->joinLeft("region as r","r.region_id = iq.area_id","region_name as area_name")
            ->where($where)->__toString ();

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }

        $data = $db->fetchAll( $sql );

        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;
    }

    public function getDeliveryShow($where ="1=1"){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        if(!$where){
            $where=" 1=1 ";
        }
        $key   = parent::_getModuleCacheKey("getDeliveryShow");
        $data = $this->_cache_handler->load($key);
        if (!empty($data))
        {
            return $data;
        }

        $sql = $db->select ()->from("iq_quotation as iq",array("delivery_area"))
            ->where($where)->__toString ();

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }

        $data = $db->fetchAll( $sql );

        $this->_cache_handler->put($key, $data, 300);//缓存5分钟
        return $data;

    }

    public function getPreprice($where,$limit='2',$type='change'){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();

        $sql = $db->select ()->from ( "iq_quotation as iq", array("price","create_time","currency_id") )
            ->where($where)
            ->order("iq.create_time desc")
            ->limit($limit)
            ->__toString ();

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        $data = $db->fetchAll( $sql );
        if($type=='change'){
            if(!empty($data)){
                if(count($data)>1){
                    $change_price =   $data[0]['price'] - $data[1]['price'];
                }else{
                    $change_price= '0';
                }
            }else{
                $change_price= '0';
            }
            return $change_price;
        }else{
            return $data;
        }
    }
}